﻿using System;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Windows.Media;

namespace SHomeWorkshop.LunarPresentation.AeroGlassLib
{
    public struct MARGINS
    {
        public MARGINS(int i)
        {
            Left = i;
            Right = i;
            Top = i;
            Bottom = i;
        }

        public MARGINS(int left, int top, int right, int bottom)
        {
            Left = left; Top = top; Right = right; Bottom = bottom;
        }

        public int Left;
        public int Right;
        public int Top;
        public int Bottom;
    }

    public class AeroGlassForWPFWindow
    {
        [DllImport("dwmapi.dll", PreserveSig = false)]
        static extern void DwmExtendFrameIntoClientArea(
            IntPtr hWnd, ref MARGINS pMarInset);
        [DllImport("dwmapi.dll", PreserveSig = false)]
        static extern bool DwmIsCompositionEnabled();

        public static bool ExtendGlassFrame(System.Windows.Window window, MARGINS margin)
        {
            if (!DwmIsCompositionEnabled())
                return false;

            IntPtr hwnd = new WindowInteropHelper(window).Handle;
            if (hwnd == IntPtr.Zero)
                throw new InvalidOperationException(
                "应用玻璃窗口特效之前要先显示窗口！");

            window.Background = System.Windows.Media.Brushes.Transparent;
            HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent;

            MARGINS refMargin = margin;
            DwmExtendFrameIntoClientArea(hwnd, ref refMargin);
            return true;
        }
    }
}
